timer on 1
use 		"$datapath/A3_gen.dta",clear

*order lopnr date firmid eventtime_def atfirm otherfirm jobsinamonth annual_ear_atfirm annual_ear_otherfirm E1 Eear1 E2 Eear2 E3 Eear3 E4 Eear4 monthly_w1 monthly_w2
 
// Get rid of employers who they worked for prior to notification 
*===============================================================================
*Firmid for employers held in a 12 month period before notification
forvalues i=1(1)10{
	qui gen xpreE`i' = E`i' if inrange(eventtime_def,-12,-1)
	qui bys lopnr (eventtime_def) : egen preE`i'=max(xpreE`i')
	drop xpreE
}

forvalues i=1(1)10{
	forvalues j=1(1)10{
		qui replace E`i' = . if preE`j'==E`i'
	}
}	

// Get rid of att additional employers who have average monthly earnings below X SEK 
*===============================================================================
*Note: 	Employer ID:s E1-E10 are sorted such that highest annual earnings (among
*	the "additional" employers) correpsond to E1 and then in decending order

*Set minimum earnings threshold
local X = 10000
forvalues i = 1(1)10{
	qui replace E`i'=. if Eear`i'<`X'  
}



{ // Generate newfirm id and employment marker
*===============================================================================
gen newfirmid = .
forvalues i=1(1)10{
	qui replace newfirmid = E`i' if newfirmid==. & eventtime_def>=0
}	



gen newfirm = newfirmid!=. 

bys lopnr (eventtime_def) : gen nvals = sum(newfirmid!=.)
gen xfirstnewfirmid = newfirmid if nvals==1
bys lopnr (eventtime_def) : egen firstnewfirmid = max(xfirstnewfirmid)
drop nvals xfirstnewfirmid
}
*
*order lopnr date firmid eventtime_def atfirm otherfirm working newfirm jobsinamonth annual_ear annual_ear_atfirm annual_ear_otherfirm firstnewfirmid newfirmid E*


{ // Merge on wages for notifyng firm
*===============================================================================
*Merge on wages for notifying firm
fmerge m:1 persid firmid year using "$datapath/A1_clean_wagesurvey_00_19.dta", keepusing(manl fastlon ovklon rorllon grlon rortup skift jourberers tjomf arbtim)
drop if _merge==2
drop _merge
}
*
{ // Merge on wages for new firms 
*===============================================================================
*Merge on wages for all(!) new jobs
preserve
	use  "$datapath/A1_clean_wagesurvey_00_19.dta",clear
	rename firmid newfirmid
	keep persid year newfirmid manl fastlon ovklon rorllon grlon rortup skift jourberers tjomf arbtim
	foreach var of varlist manl fastlon ovklon rorllon grlon rortup skift jourberers tjomf arbtim{
		rename `var' `var'_newfirm
	}	
	tempfile temp
	save 	`temp'
restore	

fmerge m:1 persid newfirmid year using `temp', keepusing(*newfirm)
drop if _merge==2
drop _merge

*Merge on wage for first(!) new job (maybe unnessecary?)
preserve
	use  "$datapath/A1_clean_wagesurvey_00_19.dta",clear
	rename firmid firstnewfirmid
	keep persid year firstnewfirmid manl
	rename manl manl_firstnewfirmid
	tempfile temp
	save 	`temp'
restore	

fmerge m:1 persid firstnewfirmid year using `temp', keepusing(manl_firstnewfirmid)
drop if _merge==2
drop _merge
}
*
order lopnr date firmid eventtime_def atfirm otherfirm working newfirm jobsinamonth annual_ear annual_ear_atfirm annual_ear_otherfirm firstnewfirmid newfirmid E*


{ // Generate new employment markers
*===============================================================================
gen otherfirm2 = newfirm
gen atfirm2 = cond(atfirm==1 & otherfirm2==0, 1,0)
gen working2 = cond(eventtime_def>=0, otherfirm2==1 | atfirm2==1	, 	otherfirm==1 | atfirm==1)

}
*



*Generate missing education level
gen educ0 =  educ1==. & educ2==. & educ3==. & educ4==.
forvalues  i=1(1)4{
	replace educ`i'=0 if educ0==1
}	
drop if educ0==1


*Fill in tenure
bys lopnr (eventtime_def) : egen x_maxtenure = max(tenure1) if eventtime<=0 & tenureatnot==.
assert tenureatnot>=x_max if x_max!=.
replace tenureatnot=x_maxtenure if tenureatnot==. & x_maxtenure!=.
bys lopnr : egen maxtenureatnot= max(tenureatnot)

gen xtemp = tenureatnot if eventtime_def==0
bys lopnr : egen maxtenureatnot= max(xtemp)
replace tenureatnot= maxtenureatnot
drop xtemp maxtenureatnot






order 	lopnr date persid varselid firmid eventtime_def working2 atfirm2 otherfirm2 annual_ear  

sort lopnr eventtime_def
compress
save "$datapath/A3_gen_empmark.dta",replace
timer off 1 
timer list
